package com.chen.corejava.collection;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 *最近最少使用:缓存
 *LRU算法是LinkedHashMap(链接映射表)本身通过双向链表来实现，当某个位置被使用(例如get操作)，通过调整链表的指向将该位置调整到头位置，
 * 新加入的内容直接放在链表头，如此一来，最近被使用的内容就向链表头移动，需要替换时(已经达到容量时)，
 * 链表最后的位置就是最近最少使用的位置
 * @param <V>
 * @param <K>
 *@author：email: <a href="cyb@ewppay.com"> Administrator </a> 
 *@Date 2017-2-21 上午10:26:51 
 */

public class MyLRUTest<K, V>{

	public void test(){
		Map<Integer,Integer> cache = new LinkedHashMap<Integer,Integer>(7,0.75F,true){//true:访问顺序，false：插入顺序
			private static final long serialVersionUID = 1L;

			@Override
			protected boolean removeEldestEntry(Entry<Integer,Integer> eldest) {
				return this.size() > 5;
			}
			
		};
		
		for(int i = 0;i < 5; i++){
			cache.put(i, i);
		}
		for(Map.Entry entry : cache.entrySet()){
			System.out.print(entry.getKey() + ",");
		}
		
		System.out.println();
		cache.get(2);
		System.out.print("after get(2) :" );
		for(Map.Entry entry : cache.entrySet()){
			System.out.print(entry.getKey() +",");
		}
		
		System.out.println();
		cache.get(4);
		System.out.print("after get(4) :");
		for(Map.Entry entry : cache.entrySet()){
			System.out.print(entry.getKey() +",");
		}
		
		System.out.println();
		cache.get(1);
		System.out.print("after get(1):" );
		for(Map.Entry entry : cache.entrySet()){
			System.out.print(entry.getKey() + ",");
		}
		
		System.out.println();
		cache.put(6, 6);
		System.out.print("after put(6):" );
		for(Map.Entry entry : cache.entrySet()){
			System.out.print(entry.getKey() + ",");
		}
		
	}
	
	public void testFalse(){
		Map<Integer,Integer> cache = new LinkedHashMap<Integer,Integer>(7,0.75F,false){//true:访问顺序，false：插入顺序
			private static final long serialVersionUID = 1L;

			@Override
			protected boolean removeEldestEntry(Entry<Integer,Integer> eldest) {
				return this.size() > 5;
			}
			
		};
		
		for(int i = 0;i < 5; i++){
			cache.put(i, i);
		}
		for(Map.Entry entry : cache.entrySet()){
			System.out.print(entry.getKey() + ",");
		}
		
		System.out.println();
		cache.get(2);
		System.out.print("after get(2) :" );
		for(Map.Entry entry : cache.entrySet()){
			System.out.print(entry.getKey() +",");
		}
		
		System.out.println();
		cache.get(4);
		System.out.print("after get(4) :");
		for(Map.Entry entry : cache.entrySet()){
			System.out.print(entry.getKey() +",");
		}
		
		System.out.println();
		cache.get(1);
		System.out.print("after get(1):" );
		for(Map.Entry entry : cache.entrySet()){
			System.out.print(entry.getKey() + ",");
		}
		
		System.out.println();
		cache.put(6, 6);
		System.out.print("after put(6):" );
		for(Map.Entry entry : cache.entrySet()){
			System.out.print(entry.getKey() + ",");
		}
		
	}
	
	/**
	 * @param @param args   
	 * @return void  
	 * @throws
	 * @author cyb
	 * @date 2017-2-21
	 */
	public static void main(String[] args) {
		MyLRUTest test = new MyLRUTest();
		test.testFalse();
		
	}

}
